﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WG计划管理工具
{
    public partial class frmWGCreate : Form
    {
        int iIdx = 0;
        int iWorkMode = 0;
        DataTable dtBrowseData;

        //获取当天最大的计划单号
        string strGetMaxPPNo = "select max(计划单号) 最大单号 from wgjha where 计划单号 like convert(varchar(100),getdate(),112)+'%'";
        //获取服务器日期
        string strGetServerDate = "select replace(CONVERT(varchar(12) , getdate(), 111),'/','-')";
        //获取服务器时间
        string strGetServerTime = "select getdate()";
        //插入单头
        string strInsertWGJHA = "insert into wgjha (计划单号) values ('@计划单号')";
        //插入单身
        string strInsertWGJHB = "insert into wgjhb (计划单号,计划项次,型号规格,WG型号规格,生产计划数量,WG计划数量,备注) values ('@计划单号',@计划项次,'@型号规格','@WG型号规格',@生产计划数量,@WG计划数量,'@备注')";
        //插入合同关联
        string strInsertWGJHHT = "insert into wgjhht (计划单号,计划项次,生产计划号,生产计划项次,生产计划数量) values ('@计划单号',@计划项次,'@生产计划号','@生产计划项次',@生产计划数量)";
        //获取单据状态
        string strSelectWGJHMark = "select 单据状态 from wgjha where 计划单号='@计划单号'";
        //更新单据状态
        string strUpdateWGJHMark = "update wgjha set 单据状态=@单据状态 where 计划单号='@计划单号'";
        //更新提交时间
        string strUpdateWGJHSubmitDate = "update wgjha set 提交时间=@提交时间 where 计划单号='@计划单号'";
        //查询所有提交状态的计划单
        string strSelectAllSubmitWGJH = "select wgjhb.* from wgjhb,wgjha where wgjhb.计划单号=wgjha.计划单号 and wgjha.单据状态=1";
        //查询指定计划单单头
        string strSelectWGJHA = "select * from wgjha where 计划单号='@计划单号'";
        //按条件查询指定计划单单身
        string strSelectWGJHBRange = "select distinct 计划单号 from wgjhb";
        //查询指定计划单单身
        string strSelectWGJHB = "select 计划项次,型号规格,WG型号规格,生产计划数量,WG计划数量,WG入库数量,备注 from wgjhb where 计划单号='@计划单号' order by 计划项次";
        //获取计划关联的第一个合同号
        string strGetContractNo = "select top 1 jhb024 from scjhb_table,wgjhht where jhb001=生产计划号 and jhb002=生产计划项次 and 计划单号='@计划单号' and 计划项次=@计划项次";
        //获取计划关联的最早的交货期
        string strGetJHQ = "select min(jhb009) from scjhb_table,wgjhht where jhb001=生产计划号 and jhb002=生产计划项次 and 计划单号='@计划单号' and 计划项次=@计划项次";
        //删除指定单号的单头
        string strDeleteWGJHA = "delete from wgjha where 计划单号='@计划单号'";
        //删除指定单号的单身
        string strDeleteWGJHB = "delete from wgjhb where 计划单号='@计划单号'";
        //获取指定计划项次关联的生产计划项次
        string strGetSCJHXC = "select 生产计划号,生产计划项次,生产计划数量 from wgjhht where 计划单号='@计划单号' and 计划项次=@计划项次";
        //删除指定单号关联的生产计划项次
        string strDeleteWGJHHT = "delete from wgjhht where 计划单号='@计划单号'";
        //查询指定生产计划数量
        string strSelectSCJHSL = "select jhb008-jhb013 from scjhb_table where jhb001='@生产计划号' and jhb002='@生产计划项次'";

        private int GetInt(object tmpobj)
        {
            int r = 0;
            try
            {
                r = Convert.ToInt32(tmpobj);
            }
            catch
            {
                r = 0;
            }
            return r;
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void ShowWarningMsg(string strTmpMsg)
        {
            MessageBox.Show(strTmpMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void ShowErrMsg(string strTmpMsg)
        {
            MessageBox.Show(strTmpMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error );
        }

        private void ShowInfo(string strTmpMsg)
        {
            MessageBox.Show(strTmpMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
        }

        public frmWGCreate()
        {
            InitializeComponent();
        }

        private void tab1_btnGetData_Click(object sender, EventArgs e)
        {
            frmPPSelector newFrmPPSelector = new frmPPSelector();
            newFrmPPSelector.ShowDialog();
            if (newFrmPPSelector.bCancel || newFrmPPSelector.dfrlst == null)
            {
                return;
            }
            if (newFrmPPSelector .dfrlst .Count >0)
            {
                tab1_dgv1.Rows.Add(newFrmPPSelector .dfrlst .Count );
                for (int i = 0; i < newFrmPPSelector.dfrlst.Count; i++)
                {
                    tab1_dgv1.Rows[tab1_dgv1.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["tab1_c2"].Value = newFrmPPSelector.dfrlst[i].strModel ;
                    tab1_dgv1.Rows[tab1_dgv1.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["tab1_c3"].Value = newFrmPPSelector.dfrlst[i].strModel;
                    tab1_dgv1.Rows[tab1_dgv1.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["tab1_c4"].Value  = newFrmPPSelector.dfrlst[i].strPPNo ;
                    tab1_dgv1.Rows[tab1_dgv1.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["tab1_c5"].Value = newFrmPPSelector.dfrlst[i].strPX ;
                    tab1_dgv1.Rows[tab1_dgv1.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["tab1_c10"].Value = newFrmPPSelector.dfrlst[i].iQuantity;
                    tab1_dgv1.Rows[tab1_dgv1.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["tab1_c6"].Value = newFrmPPSelector.dfrlst[i].iSum ;
                    tab1_dgv1.Rows[tab1_dgv1.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["tab1_c7"].Value = newFrmPPSelector.dfrlst[i].iSum ;
                    tab1_dgv1.Rows[tab1_dgv1.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["tab1_c9"].Value = newFrmPPSelector .dfrlst [i].strMemo ;
                }
                tab1_dgv1.AutoResizeColumns();
                FillRowIndex();
            }
        }

        private void tab1_btnAdd_Click(object sender, EventArgs e)
        {
            iWorkMode = 1;                                                                //设置工作模式为新增模式
            tab1_btnSearch.Enabled = false;
            tab1_btnAdd.Enabled = false;
            tab1_btnConfirm.Enabled = true;
            tab1_btnEdit.Enabled = false;
            tab1_btnCancel.Enabled = true;
            tab1_btnDelete.Enabled = false;
            tab1_btnSubmit.Enabled = false;
            tab1_btnGetData.Enabled = true;
            tab1_btnFirst.Enabled = false;
            tab1_btnPrevious.Enabled = false;
            tab1_btnNext.Enabled = false;
            tab1_btnLast.Enabled = false;
            tab1_txtWGPPNo .Text = GetPPNo();
            tab1_dgv1.ReadOnly = false;
            tab1_lblMark.Text = "";
            tab1_lblMark.Visible = false;
            tab1_dgv1.ContextMenuStrip = contextMenuStrip1;
            tab1_dgv1.DataSource = null;
            tab1_dgv1.Rows.Clear();
        }

        private string GetPPNo()
        {
            SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
            string strTmpMaxPPNo = GetString ( sqt.DoCmdScalarData(strGetMaxPPNo));
            string strTmpDate = string.Empty;
            string strTmpNo = string.Empty;
            if (strTmpMaxPPNo.Trim().Length == 0)
            {
                strTmpDate = GetString(sqt.DoCmdScalarData(strGetServerDate)).Replace ("-","");
                strTmpNo = "0001";
            }
            else
            {
                strTmpDate = strTmpMaxPPNo.Substring(0, 8);
                int iTmpNo = GetInt(strTmpMaxPPNo.Substring(8, 4)) + 1;
                strTmpNo = iTmpNo .ToString ("0000");
            }
            return strTmpDate + strTmpNo;
        }

        private void FillRowIndex()
        {
            if (tab1_dgv1.Rows.Count > 0)
            {
                for (int i = 0; i < tab1_dgv1.Rows.Count; i++)
                {
                    tab1_dgv1.Rows[i].Cells["tab1_c1"].Value = i + 1;
                }
            }
        }

        private void tab1_dgv1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            FillRowIndex();
        }

        private void tab1_dgv1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            FillRowIndex();
        }

        private void 添加行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tab1_dgv1.Rows.Add(1);
            tab1_dgv1.CurrentCell = tab1_dgv1.Rows[tab1_dgv1.Rows.Count - 1].Cells[1];
            FillRowIndex();
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tab1_dgv1.CurrentRow != null)
            {
                DialogResult dr = MessageBox.Show("确定删除序号为" + GetString(tab1_dgv1.CurrentRow.Cells["tab1_c1"].Value) + "的行吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    tab1_dgv1.Rows.Remove(tab1_dgv1.CurrentRow);
                    FillRowIndex();
                }
            }
        }

        private void tab1_btnSubmit_Click(object sender, EventArgs e)
        {
            SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
            int iMark = GetInt(sqt.DoCmdScalarData(strSelectWGJHMark.Replace("@计划单号", tab1_txtWGPPNo.Text)));
            if (iMark == 0)
            {
                sqt.DoCmdExecuteNonQuery(strUpdateWGJHMark.Replace("@计划单号", tab1_txtWGPPNo.Text)
                                                                                                                .Replace("@单据状态", "1"));
                sqt.DoCmdExecuteNonQuery(strUpdateWGJHSubmitDate .Replace("@计划单号", tab1_txtWGPPNo.Text)
                                                                                                                                .Replace("@提交时间", "replace(CONVERT(varchar(12) , getdate(), 111),'/','-')"));
                tab1_txtSubmitDate.Text = GetString(sqt.DoCmdScalarData(strGetServerDate));
                tab1_lblMark.Text = "提";
                tab1_lblMark.Visible = true;
                tab1_btnEdit.Enabled = false;
                tab1_btnDelete.Enabled = false;
                tab1_btnCancel.Enabled = false;
                tab1_btnConfirm.Enabled = false;
            }
            else
            {
                if (iMark == 1)
                {
                    sqt.DoCmdExecuteNonQuery(strUpdateWGJHMark.Replace("@计划单号", tab1_txtWGPPNo.Text)
                                                                                                                .Replace("@单据状态", "0"));
                    sqt.DoCmdExecuteNonQuery(strUpdateWGJHSubmitDate.Replace("@计划单号", tab1_txtWGPPNo.Text)
                                                                                                                                .Replace("@提交时间", "null"));
                    tab1_txtSubmitDate.Clear();
                    tab1_lblMark.Text = "";
                    tab1_lblMark.Visible = false ;
                    tab1_btnEdit.Enabled = true;
                    tab1_btnDelete.Enabled = true ;
                    tab1_btnCancel.Enabled = false;
                    tab1_btnConfirm.Enabled = false;
                }
            }
        }

        private void tab2_rbDateRange_CheckedChanged(object sender, EventArgs e)
        {
            tab2_dtpFrom.Enabled = tab2_rbDateRange.Checked;
            tab2_dtpTo.Enabled = tab2_rbDateRange.Checked;
        }

        private void tab2_rbModel_CheckedChanged(object sender, EventArgs e)
        {
            tab2_txtModel.Enabled = tab2_rbModel.Checked;
        }

        private void tab2_rbContractNo_CheckedChanged(object sender, EventArgs e)
        {
            tab2_txtContract.Enabled = tab2_rbContractNo.Checked;
        }

        private void tab2_btnSearch_Click(object sender, EventArgs e)
        {
            string strTmpSelectString = strSelectAllSubmitWGJH;
            if (tab2_rbDateRange.Checked)
            {
                strTmpSelectString += " and 提交时间 between '" + tab2_dtpFrom.Value.ToString("yyyy-MM-dd") + "' and '" + tab2_dtpTo.Value.ToString("yyyy-MM-dd")+"'";
            }
            if (tab2_rbModel.Checked)
            {
                if (tab2_txtModel.Text.Trim().Length == 0)
                {
                    ShowWarningMsg("请填写要查询的型号规格！");
                    tab2_txtModel.Focus();
                    return;
                }
                strTmpSelectString += " and 型号规格 like '" + tab2_txtModel.Text.Trim() + "'";
            }
            if (tab2_rbContractNo.Checked)
            {
                if (tab2_txtContract.Text.Trim().Length == 0)
                {
                    ShowWarningMsg("请填写要查询的合同号！");
                    tab2_txtContract.Focus();
                    return;
                }
                strTmpSelectString += "and wgjhb.计划单号 in (select 计划单号 from wgjhht,scjhb_table where 生产计划号=jhb001 and jhb024 like '"+tab2_txtContract .Text .Trim ()+"')";
            }
            strTmpSelectString +=" order by WG型号规格";
            SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
            DataTable tmpdt = sqt.GetDataTable(strTmpSelectString);
            tab2_dgv1.DataSource = tmpdt;
            tab2_dgv1.AutoResizeColumns();
        }

        private void tab2_btnClear_Click(object sender, EventArgs e)
        {
            tab2_txtModel.Clear();
            tab2_txtContract.Clear();
            tab2_dgv1.DataSource = null;
        }

        private void tab2_dgv1_SelectionChanged(object sender, EventArgs e)
        {
            tab2_lblSelectCount.Text = "已选定" + tab2_dgv1.SelectedRows.Count.ToString() + "行";
        }

        private void tab2_btnExport_Click(object sender, EventArgs e)
        {
            if (tab2_dgv1.Rows.Count == 0)
            {
                ShowWarningMsg("表中没有可以导出的数据！");
                return;
            }
            if (tab2_dgv1.SelectedRows.Count == 0)
            {
                ShowWarningMsg("请先选择要导出的数据！");
                return;
            }
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "*.xls|*.xls";
            sfd.ShowDialog();
            string strTmpFileName = sfd.FileName;
            if (strTmpFileName.Trim().Length > 0)
            {
                DataTable tmpdt = new DataTable();
                tmpdt.Columns.Add("序号");
                tmpdt.Columns.Add("等级");
                tmpdt.Columns.Add("合同号");
                tmpdt.Columns.Add("型号");
                tmpdt.Columns.Add("端电极");
                tmpdt.Columns.Add("合同数量");
                tmpdt.Columns.Add("申购数量");
                tmpdt.Columns.Add("合同交期");
                tmpdt.Columns.Add("计划日期");
                tmpdt.Columns.Add("对应配套型号");
                tmpdt.Columns.Add("实际订量");
                tmpdt.Columns.Add("到货日期");
                tmpdt.Columns.Add("到货数量");
                tmpdt.Columns.Add("欠交数量");
                tmpdt.Columns.Add("计划处理情况");
                tmpdt.Columns.Add("供应商回复情况");
                tmpdt.Columns.Add("备注");
                SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
                for (int i = 0; i < tab2_dgv1.SelectedRows.Count; i++)
                {
                    DataRow r = tmpdt.NewRow();
                    r["序号"] = Convert.ToString(i + 1);
                    r["等级"] = tab2_dgv1.SelectedRows[i].Cells["型号规格"].Value.ToString().Substring(0, tab2_dgv1.SelectedRows[i].Cells["型号规格"].Value.ToString().IndexOf('-'));
                    r["合同号"] = GetString(sqt.DoCmdScalarData(strGetContractNo.Replace("@计划单号", tab2_dgv1.Rows[i].Cells["计划单号"].Value.ToString().Trim())
                                                                                                                                        .Replace("@计划项次", tab2_dgv1.Rows[i].Cells["计划项次"].Value.ToString().Trim())));
                    r["合同交期"] = GetString(sqt.DoCmdScalarData(strGetJHQ .Replace("@计划单号", tab2_dgv1.Rows[i].Cells["计划单号"].Value.ToString().Trim())
                                                                                                                                        .Replace("@计划项次", tab2_dgv1.Rows[i].Cells["计划项次"].Value.ToString().Trim())));
                    if (r["合同交期"].ToString().Trim().Length ==8)
                    {
                        r["合同交期"] = r["合同交期"].ToString().Trim().Insert(6, "-").Insert(4, "-");
                    }
                    if (r["等级"].ToString().Trim() == "M")
                    {
                        r["等级"] = "工业级";
                    }
                    r["端电极"] = "三端";
                    r["计划日期"] = DateTime.Now.ToString("yyyy-MM-dd");
                    r["型号"] = tab2_dgv1.SelectedRows[i].Cells["WG型号规格"].Value;
                    r["合同数量"] = tab2_dgv1.SelectedRows[i].Cells["生产计划数量"].Value.ToString();
                    r["申购数量"] = tab2_dgv1.SelectedRows[i].Cells["WG计划数量"].Value.ToString();
                    r["备注"] = tab2_dgv1.SelectedRows[i].Cells["备注"].Value.ToString();
                    tmpdt.Rows.Add(r);
                }
                ExcelHelper.DataToExcel dte = new ExcelHelper.DataToExcel();
                if (dte.ExportToExcel(tmpdt, strTmpFileName, true, "外购申购表" ))
                {
                    ShowInfo("EXCEL文件导出成功！");
                }
                else
                {
                    ShowErrMsg(dte.strErrMsg);
                }
            }
        }

        private void tab1_btnSearch_Click(object sender, EventArgs e)
        {
            SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties .Settings.Default .ConnectionString );
            string strTmpSelectStringB = strSelectWGJHBRange;
            if (tab1_rbtn1.Checked&&tab1_txtPPNo .Text .Trim ().Length >0)
            {
                strTmpSelectStringB += " where 计划单号 like '" + tab1_txtPPNo.Text.Trim() + "'";
            }
            if (tab1_rbtn2.Checked&&tab1_txtModel .Text .Trim ().Length >0)
            {
                strTmpSelectStringB += " where WG型号规格 like '" + tab1_txtModel.Text.Trim() + "'";
            }
            dtBrowseData = sqt.GetDataTable(strTmpSelectStringB);
            if (dtBrowseData.Rows.Count > 0)
            {
                iIdx = 0;
                if (dtBrowseData.Rows.Count > 1)
                {
                    tab1_btnFirst.Enabled = false;
                    tab1_btnPrevious.Enabled = false;
                    tab1_btnNext.Enabled = true;
                    tab1_btnLast.Enabled = true;
                }
                else
                {
                    tab1_btnFirst.Enabled = false;
                    tab1_btnPrevious.Enabled = false;
                    tab1_btnNext.Enabled = false;
                    tab1_btnLast.Enabled = false;
                }
                ReadData();
            }
            else
            {
                tab1_txtWGPPNo.Clear();
                tab1_txtSubmitDate.Clear();
                tab1_txtAuditDate.Clear();
                tab1_txtComplishDate.Clear();
                tab1_dgv1.Rows.Clear();
                tab1_btnFirst.Enabled = false;
                tab1_btnPrevious.Enabled = false;
                tab1_btnNext.Enabled = false;
                tab1_btnLast.Enabled = false;
                tab1_lblMark.Text = "";
                tab1_lblMark.Visible = false;
                tab1_btnSubmit.Enabled = false;
                tab1_btnGetData.Enabled = false;
            }
        }

        private void ReadData()
        {
            if (dtBrowseData != null)
            {
                tab1_btnAdd.Enabled = true;
                tab1_btnEdit.Enabled = false;
                tab1_btnDelete.Enabled = false;
                tab1_btnConfirm.Enabled = false;
                tab1_btnCancel.Enabled = false;
                tab1_btnSubmit.Enabled = false;
                tab1_btnGetData.Enabled = false;
                if (dtBrowseData.Rows.Count == 0)
                {
                    tab1_txtWGPPNo.Clear();
                    tab1_txtSubmitDate.Clear();
                    tab1_txtAuditDate.Clear();
                    tab1_txtComplishDate.Clear();
                    return;
                }
                SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
                DataTable tmpdta = sqt.GetDataTable(strSelectWGJHA.Replace("@计划单号", GetString(dtBrowseData.Rows[iIdx]["计划单号"])));
                DataTable tmpdtb = sqt.GetDataTable(strSelectWGJHB.Replace("@计划单号", GetString(dtBrowseData.Rows[iIdx]["计划单号"])));
                tab1_txtWGPPNo.Text = GetString(tmpdta.Rows[0]["计划单号"]).Trim ();
                tab1_txtSubmitDate.Text = GetString(tmpdta.Rows[0]["提交时间"]).Trim ();
                tab1_txtAuditDate.Text = GetString(tmpdta.Rows[0]["审核时间"]).Trim ();
                tab1_txtComplishDate.Text  = GetString(tmpdta.Rows[0]["完成时间"]).Trim ();
                switch (GetString(tmpdta.Rows[0]["单据状态"]).Trim())
                {
                    case "0":
                        tab1_lblMark.Text = "";
                        tab1_lblMark.Visible = false;
                        tab1_btnSubmit.Enabled = true;
                        tab1_btnDelete.Enabled = true;
                        tab1_btnEdit.Enabled = true;
                        break;
                    case "1":
                        tab1_lblMark.Text = "提";
                        tab1_lblMark.Visible = true ;
                        tab1_btnSubmit.Enabled = true  ;
                        tab1_btnDelete.Enabled = false ;
                        tab1_btnEdit.Enabled = false;
                        break;
                    case "2":
                        tab1_lblMark.Text = "核";
                        tab1_lblMark.Visible = true;
                        tab1_btnSubmit.Enabled = false;
                        tab1_btnDelete.Enabled = false;
                        tab1_btnEdit.Enabled = false;
                        break;
                    case "3":
                        tab1_lblMark.Text = "完";
                        tab1_lblMark.Visible = true;
                        tab1_btnSubmit.Enabled = false;
                        tab1_btnDelete.Enabled = false;
                        tab1_btnEdit.Enabled = false;
                        break;
                }
                tab1_dgv1.DataSource = null;
                tab1_dgv1 .Rows.Clear ();
                if (tmpdtb.Rows.Count > 0)
                {
                    tab1_dgv1.Rows.Add(tmpdtb.Rows.Count);
                    for (int i = 0; i < tmpdtb.Rows.Count; i++)
                    {
                        tab1_dgv1.Rows[i].Cells["tab1_c1"].Value = tmpdtb.Rows[i]["计划项次"];
                        tab1_dgv1.Rows[i].Cells["tab1_c2"].Value = tmpdtb.Rows[i]["型号规格"];
                        tab1_dgv1.Rows[i].Cells["tab1_c3"].Value = tmpdtb.Rows[i]["WG型号规格"];
                        string[] strTmpPP=new string [1];
                        string[] strTmpPX=new string [1];
                        int[] iTmpQuantity=new int [1];
                        DataTable tmpdtscjh = sqt.GetDataTable(strGetSCJHXC.Replace("@计划单号", tab1_txtWGPPNo.Text)
                                                                                                                .Replace("@计划项次", tmpdtb.Rows[i]["计划项次"].ToString()));
                        if (tmpdtscjh.Rows.Count > 0)
                        {
                            strTmpPP = new string[tmpdtscjh.Rows.Count];
                            strTmpPX = new string[tmpdtscjh.Rows.Count];
                            iTmpQuantity = new int[tmpdtscjh.Rows.Count];
                            for (int k = 0; k < tmpdtscjh.Rows.Count; k++)
                            {
                                strTmpPP[k] = tmpdtscjh.Rows[k]["生产计划号"].ToString().Trim();
                                strTmpPX[k] = tmpdtscjh.Rows[k]["生产计划项次"].ToString().Trim();
                                iTmpQuantity[k] =GetInt(tmpdtscjh.Rows[k]["生产计划数量"].ToString());
                            }
                        }
                        tab1_dgv1.Rows[i].Cells["tab1_c4"].Value=strTmpPP ;
                        tab1_dgv1.Rows[i].Cells["tab1_c5"].Value = strTmpPX;
                        tab1_dgv1.Rows[i].Cells["tab1_c10"].Value = iTmpQuantity ;
                        tab1_dgv1.Rows[i].Cells["tab1_c6"].Value = tmpdtb.Rows[i]["生产计划数量"];
                        tab1_dgv1.Rows[i].Cells["tab1_c7"].Value = tmpdtb.Rows[i]["WG计划数量"];
                        tab1_dgv1.Rows[i].Cells["tab1_c8"].Value = tmpdtb.Rows[i]["WG入库数量"];
                        tab1_dgv1.Rows[i].Cells["tab1_c9"].Value = tmpdtb.Rows[i]["备注"];
                    }
                    tab1_dgv1.AutoResizeColumns();
                }
            }
        }

        private void tab1_rbtn1_CheckedChanged(object sender, EventArgs e)
        {
            tab1_rbtn2.Checked = !tab1_rbtn1 .Checked ;
            tab1_txtPPNo.Enabled = tab1_rbtn1.Checked;
        }

        private void tab1_rbtn2_CheckedChanged(object sender, EventArgs e)
        {
            tab1_rbtn1.Checked = !tab1_rbtn2.Checked;
            tab1_txtModel.Enabled = tab1_rbtn2.Checked;
        }

        private void tab1_btnConfirm_Click(object sender, EventArgs e)
        {
            switch (iWorkMode)
            {
                case 1:
                    if (tab1_dgv1.Rows.Count == 0)
                    {
                        ShowWarningMsg("单身没有内容，无法保存该单据！");
                        return;
                    }
                    else
                    {
                        for (int i = tab1_dgv1.Rows.Count - 1; i >= 0; i--)
                        {
                            bool bBlankRow = true;
                            foreach (DataGridViewCell tmpdgvc in tab1_dgv1.Rows[i].Cells)
                            {
                                if (tmpdgvc.ColumnIndex == 0)
                                {
                                    continue;
                                }
                                if (GetString(tmpdgvc.Value).Trim().Length > 0)
                                {
                                    bBlankRow = false;
                                    break;
                                }
                            }
                            if (!bBlankRow)
                            {
                                break;
                            }
                            else
                            {
                                tab1_dgv1.Rows.RemoveAt(i);
                            }
                        }
                        FillRowIndex();
                        if (tab1_dgv1.Rows.Count == 0)
                        {
                            ShowWarningMsg("单身没有内容，无法保存该单据！");
                            return;
                        }
                        else
                        {
                            for (int i = 0; i < tab1_dgv1.Rows.Count; i++)
                            {
                                if (GetString(tab1_dgv1.Rows[i].Cells["tab1_c3"].Value).Trim().Length == 0 || GetString(tab1_dgv1.Rows[i].Cells["tab1_c7"].Value).Trim().Length == 0)
                                {
                                    ShowWarningMsg("采购型号规格和采购数量不允许为空！");
                                    return;
                                }
                            }
                            SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
                            tab1_txtWGPPNo.Text = GetPPNo();
                            if (sqt.DoCmdExecuteNonQuery(strInsertWGJHA.Replace("@计划单号", tab1_txtWGPPNo.Text.Trim())) > 0)
                            {
                                for (int i = 0; i < tab1_dgv1.Rows.Count; i++)
                                {
                                    if (tab1_dgv1.Rows[i].Cells["tab1_c4"].Value!=null &&tab1_dgv1.Rows[i].Cells["tab1_c5"].Value!=null&&tab1_dgv1.Rows[i].Cells["tab1_c10"].Value!=null)
                                    {
                                        string[] strTmpPP =(string[])tab1_dgv1.Rows[i].Cells["tab1_c4"].Value;
                                        string[] strTmpPX = (string[])tab1_dgv1.Rows[i].Cells["tab1_c5"].Value;
                                        int[] iTmpQuantity = (int[])tab1_dgv1.Rows[i].Cells["tab1_c10"].Value;
                                        for (int k = 0; k < strTmpPP.Length; k++)
                                        {
                                            if (strTmpPP[k].Trim().Length == 0)
                                            {
                                                continue;
                                            }
                                               sqt.DoCmdExecuteNonQuery(strInsertWGJHHT.Replace("@计划单号", tab1_txtWGPPNo.Text.Trim())
                                                                                                                                .Replace("@计划项次", GetString(tab1_dgv1.Rows[i].Cells["tab1_c1"].Value))
                                                                                                                                .Replace("@生产计划号", strTmpPP[k].Trim())
                                                                                                                                .Replace("@生产计划项次", strTmpPX[k].Trim())
                                                                                                                                .Replace ("@生产计划数量",iTmpQuantity [k].ToString ()));
                                        }
                                    }
                                    sqt.DoCmdExecuteNonQuery(strInsertWGJHB.Replace("@计划单号", tab1_txtWGPPNo.Text.Trim())
                                                                                                                         .Replace("@计划项次", GetString(tab1_dgv1.Rows[i].Cells["tab1_c1"].Value))
                                                                                                                        .Replace("@型号规格", GetString(tab1_dgv1.Rows[i].Cells["tab1_c2"].Value))
                                                                                                                        .Replace("@WG型号规格", GetString(tab1_dgv1.Rows[i].Cells["tab1_c3"].Value))
                                                                                                                        .Replace("@生产计划数量", GetString(tab1_dgv1.Rows[i].Cells["tab1_c6"].Value))
                                                                                                                        .Replace("@WG计划数量", GetString(tab1_dgv1.Rows[i].Cells["tab1_c7"].Value))
                                                                                                                        .Replace("@备注", GetString(tab1_dgv1.Rows[i].Cells["tab1_c9"].Value)));
                                }
                            }
                            tab1_btnAdd.Enabled = true;
                            tab1_btnConfirm.Enabled = false;
                            tab1_btnCancel.Enabled = false;
                            tab1_btnDelete.Enabled = true;
                            tab1_btnSubmit.Enabled = true;
                            tab1_btnGetData.Enabled = false;
                            tab1_btnFirst.Enabled = false;
                            tab1_btnPrevious.Enabled = false;
                            tab1_btnNext.Enabled = false;
                            tab1_btnLast.Enabled = false;
                            tab1_dgv1.ReadOnly = true;
                            tab1_dgv1.ContextMenuStrip = null;
                            iWorkMode = 0;
                        }
                    }
                    break;
                case 2:
                    if (tab1_dgv1.Rows.Count == 0)
                    {
                        ShowWarningMsg("单身没有内容，无法保存该单据！");
                        return;
                    }
                    else
                    {
                        for (int i = tab1_dgv1.Rows.Count - 1; i >= 0; i--)
                        {
                            bool bBlankRow = true;
                            foreach (DataGridViewCell tmpdgvc in tab1_dgv1.Rows[i].Cells)
                            {
                                if (tmpdgvc.ColumnIndex == 0)
                                {
                                    continue;
                                }
                                if (GetString(tmpdgvc.Value).Trim().Length > 0)
                                {
                                    bBlankRow = false;
                                    break;
                                }
                            }
                            if (!bBlankRow)
                            {
                                break;
                            }
                            else
                            {
                                tab1_dgv1.Rows.RemoveAt(i);
                            }
                        }
                        FillRowIndex();
                        if (tab1_dgv1.Rows.Count == 0)
                        {
                            ShowWarningMsg("单身没有内容，无法保存该单据！");
                            return;
                        }
                        else
                        {
                            for (int i = 0; i < tab1_dgv1.Rows.Count; i++)
                            {
                                if (GetString(tab1_dgv1.Rows[i].Cells["tab1_c3"].Value).Trim().Length == 0 || GetString(tab1_dgv1.Rows[i].Cells["tab1_c7"].Value).Trim().Length == 0)
                                {
                                    ShowWarningMsg("采购型号规格和采购数量不允许为空！");
                                    return;
                                }
                            }
                            SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
                            sqt.DoCmdExecuteNonQuery(strDeleteWGJHB.Replace("@计划单号", tab1_txtWGPPNo.Text));
                            sqt.DoCmdExecuteNonQuery(strDeleteWGJHHT.Replace("@计划单号", tab1_txtWGPPNo.Text));
                            for (int i = 0; i < tab1_dgv1.Rows.Count; i++)
                            {
                                if (tab1_dgv1.Rows[i].Cells["tab1_c4"].Value!=null &&tab1_dgv1.Rows[i].Cells["tab1_c5"].Value!=null&&tab1_dgv1.Rows[i].Cells["tab1_c10"].Value!=null)
                                {
                                    string[] strTmpPP =(string[])tab1_dgv1.Rows[i].Cells["tab1_c4"].Value;
                                    string[] strTmpPX = (string[])tab1_dgv1.Rows[i].Cells["tab1_c5"].Value;
                                    int[] iTmpQuantity = (int[])tab1_dgv1.Rows[i].Cells["tab1_c10"].Value;
                                    for (int k = 0; k < strTmpPP.Length; k++)
                                    {
                                        if (strTmpPP[k].Trim().Length == 0)
                                        {
                                            continue;
                                        }
                                       sqt.DoCmdExecuteNonQuery(strInsertWGJHHT.Replace("@计划单号", tab1_txtWGPPNo.Text.Trim())
                                                                                                                        .Replace("@计划项次", GetString(tab1_dgv1.Rows[i].Cells["tab1_c1"].Value))
                                                                                                                        .Replace("@生产计划号", strTmpPP[k].Trim())
                                                                                                                        .Replace("@生产计划项次", strTmpPX[k].Trim())
                                                                                                                        .Replace ("@生产计划数量",iTmpQuantity [k].ToString ()));
                                    }
                                }
                                sqt.DoCmdExecuteNonQuery(strInsertWGJHB.Replace("@计划单号", tab1_txtWGPPNo.Text.Trim())
                                                                                                                     .Replace("@计划项次", GetString(tab1_dgv1.Rows[i].Cells["tab1_c1"].Value))
                                                                                                                    .Replace("@型号规格", GetString(tab1_dgv1.Rows[i].Cells["tab1_c2"].Value))
                                                                                                                    .Replace("@WG型号规格", GetString(tab1_dgv1.Rows[i].Cells["tab1_c3"].Value))
                                                                                                                    .Replace("@生产计划数量", GetString(tab1_dgv1.Rows[i].Cells["tab1_c6"].Value))
                                                                                                                    .Replace("@WG计划数量", GetString(tab1_dgv1.Rows[i].Cells["tab1_c7"].Value))
                                                                                                                    .Replace("@备注", GetString(tab1_dgv1.Rows[i].Cells["tab1_c9"].Value)));
                            }
                            tab1_btnAdd.Enabled = true;
                            tab1_btnConfirm.Enabled = false;
                            tab1_btnCancel.Enabled = false;
                            tab1_btnDelete.Enabled = true;
                            tab1_btnSubmit.Enabled = true;
                            tab1_btnGetData.Enabled = false;
                            tab1_btnFirst.Enabled = false;
                            tab1_btnPrevious.Enabled = false;
                            tab1_btnNext.Enabled = false;
                            tab1_btnLast.Enabled = false;
                            tab1_dgv1.ReadOnly = true;
                            tab1_dgv1.ContextMenuStrip = null;
                            iWorkMode = 0;
                        }
                    }
                    break;
            }
        }

        private void tab1_btnCancel_Click(object sender, EventArgs e)
        {
            switch (iWorkMode)
            {
                case 1:
                    iWorkMode = 0;
                    tab1_txtWGPPNo.Clear();
                    tab1_txtSubmitDate.Clear();
                    tab1_txtAuditDate.Clear();
                    tab1_txtComplishDate.Clear();
                    tab1_btnAdd.Enabled = true;
                    tab1_btnEdit.Enabled = false;
                    tab1_btnDelete.Enabled = false;
                    tab1_btnConfirm.Enabled = false;
                    tab1_btnCancel.Enabled = false;
                    tab1_btnSubmit.Enabled = false;
                    tab1_btnGetData.Enabled = false;
                    tab1_dgv1.Rows.Clear();
                    tab1_dgv1.ReadOnly = true;
                    tab1_dgv1.ContextMenuStrip = null;
                    break;
                case 2:
                    iWorkMode = 0;
                    tab1_txtWGPPNo.Clear();
                    tab1_txtSubmitDate.Clear();
                    tab1_txtAuditDate.Clear();
                    tab1_txtComplishDate.Clear();
                    tab1_btnAdd.Enabled = true;
                    tab1_btnEdit.Enabled = false;
                    tab1_btnDelete.Enabled = false;
                    tab1_btnConfirm.Enabled = false;
                    tab1_btnCancel.Enabled = false;
                    tab1_btnSubmit.Enabled = false;
                    tab1_btnGetData.Enabled = false;
                    tab1_dgv1.Rows.Clear();
                    tab1_dgv1.ReadOnly = true;
                    tab1_dgv1.ContextMenuStrip = null;
                    ReadData();
                    break;
            }
        }

        private void tab1_btnFirst_Click(object sender, EventArgs e)
        {
            iIdx = 0;
            tab1_btnFirst.Enabled = false;
            tab1_btnPrevious.Enabled = false;
            tab1_btnNext.Enabled = true;
            tab1_btnLast.Enabled = true;
            ReadData();
        }

        private void tab1_btnPrevious_Click(object sender, EventArgs e)
        {
            iIdx--;
            if (iIdx == 0)
            {
                tab1_btnFirst.Enabled = false;
                tab1_btnPrevious.Enabled = false;
            }
            else
            {
                tab1_btnFirst.Enabled = true;
                tab1_btnPrevious.Enabled = true;
            }
            tab1_btnNext.Enabled = true;
            tab1_btnLast.Enabled = true;
            ReadData();
        }

        private void tab1_btnNext_Click(object sender, EventArgs e)
        {
            iIdx++;
            if (iIdx == dtBrowseData .Rows.Count -1)
            {
                tab1_btnLast.Enabled = false;
                tab1_btnNext.Enabled = false;
            }
            else
            {
                tab1_btnLast.Enabled = true;
                tab1_btnNext.Enabled = true;
            }
            tab1_btnPrevious.Enabled = true;
            tab1_btnFirst.Enabled = true;
            ReadData();
        }

        private void tab1_btnLast_Click(object sender, EventArgs e)
        {
            iIdx = dtBrowseData .Rows .Count -1;
            tab1_btnFirst.Enabled = true ;
            tab1_btnPrevious.Enabled = true ;
            tab1_btnNext.Enabled = false ;
            tab1_btnLast.Enabled = false ;
            ReadData();
        }

        private void tab2_btnSelectAll_Click(object sender, EventArgs e)
        {
            if (tab2_dgv1.Rows.Count > 0)
            {
                for (int i = 0; i < tab2_dgv1.Rows.Count; i++)
                {
                    tab2_dgv1.Rows[i].Selected = true;
                }
            }
        }

        private void tab1_btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定删除单号为" + tab1_txtWGPPNo.Text + "的计划单？删除以后将无法恢复！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                SqlTools.SqlTools sqt = new SqlTools.SqlTools ( WG计划管理工具.Properties.Settings.Default.ConnectionString);
                sqt.DoCmdExecuteNonQuery(strDeleteWGJHA.Replace("@计划单号", tab1_txtWGPPNo.Text));
                sqt.DoCmdExecuteNonQuery(strDeleteWGJHB.Replace("@计划单号", tab1_txtWGPPNo.Text));
                if (dtBrowseData.Rows.Count > 0)
                {
                    dtBrowseData.Rows.RemoveAt(iIdx);
                    if (iIdx > dtBrowseData.Rows.Count - 1)
                    {
                        iIdx = dtBrowseData.Rows.Count - 1;
                    }
                    ReadData();
                }
            }
        }

        private void tab1_btnEdit_Click(object sender, EventArgs e)
        {
            iWorkMode = 2;                                                                //设置工作模式为编辑模式
            tab1_btnSearch.Enabled = false;
            tab1_btnAdd.Enabled = false;
            tab1_btnConfirm.Enabled = true;
            tab1_btnEdit.Enabled = false;
            tab1_btnCancel.Enabled = true;
            tab1_btnDelete.Enabled = false;
            tab1_btnSubmit.Enabled = false;
            tab1_btnGetData.Enabled = true;
            tab1_btnFirst.Enabled = false;
            tab1_btnPrevious.Enabled = false;
            tab1_btnNext.Enabled = false;
            tab1_btnLast.Enabled = false;
            tab1_dgv1.ReadOnly = false;
            tab1_lblMark.Text = "";
            tab1_lblMark.Visible = false;
            tab1_dgv1.ContextMenuStrip = contextMenuStrip1;
        }

        private void tab1_dgv1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (tab1_dgv1 .CurrentCell !=null && tab1_dgv1.Columns[e.ColumnIndex].HeaderText == "生产计划数量")
            {
                frmJHHTBrowse newFrmJHHTBrowse = new frmJHHTBrowse(tab1_txtWGPPNo.Text, GetString(tab1_dgv1.CurrentRow.Cells["tab1_c1"].Value));
                newFrmJHHTBrowse.ShowDialog();
            }
        }

        private void frmWGCreate_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void frmWGCreate_Load(object sender, EventArgs e)
        {
        }
    }
}